//
//  XQRouteTool.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit
import Foundation

class XQRouteTool: NSObject {

    static func jumpLoginController(controller : UIViewController) {
        let vc = XQLoginController()
        controller.navigationController?.pushViewController(vc, animated: true)
    }
    
    static func jumpController(controller : UIViewController,url : String) {
        if (url.hasPrefix("http")) {
           jumpWebController(controller: controller, url: url)
        }else if (url.contains("spa://rkp.eso/feel")){ // login
            
        }else if (url.contains("spa://rkp.eso/are")) { // home
            
        }else if (url.contains("spa://rkp.eso/ethnicnational")) { // setting
            let vc = XQSettingController()
            controller.navigationController?.pushViewController(vc, animated: true)
        }else if (url.contains("spa://rkp.eso/between")) {
            let arr = url.components(separatedBy: "?")
            if (arr.count == 2) {
                let accommodate = arr.last!
                let valueArr = accommodate.components(separatedBy: "=")
                if valueArr.count == 2 {
                    let orderId = valueArr.last!
                    if !orderId.isEmpty {
                        jumpDetail(orderId: orderId, controller: controller)
                    }
                }
            }
        }
    }
    
    static func findTabController() -> XQMainTabBarController? {
        guard let rootNav = UIApplication.shared.delegate?.window??.rootViewController as? UINavigationController else {
            return nil
        }
        guard let homeTabController = rootNav.viewControllers.first as? XQMainTabBarController else {
            return nil
        }
        return homeTabController
    }
    
    static func jumpWebController(controller : UIViewController,url : String) {
        let vc = XQWebController()
        vc.url = XQNetworkManager.shared.getWebUrlAbsolutePath(path: url)
        controller.navigationController?.pushViewController(vc, animated: true)
    }
    
    static func jumpHome() {
        if let tabController = findTabController() {
            tabController.goHomeController()
        }
    }
    
    static func jumpMine() {
        if let tabController = findTabController() {
            tabController.goMineController()
        }
    }
    
    static func rootController() -> UIViewController? {
        guard let rootVC = UIApplication.shared.delegate?.window??.rootViewController else {
            return nil
        }
        var topVC = rootVC
        // 递归查找最上层的视图控制器
        while let presentedVC = topVC.presentedViewController {
            topVC = presentedVC
        }
        return topVC
    }
    
    static func jumpLogin() {
        if let controller = rootController() {
            let loginVC = XQLoginController()
            (controller as! UINavigationController).pushViewController(loginVC, animated: true)
        }
    }
    
    static func jumpDetail(orderId : String,controller : UIViewController) {
        let vc = XQOrderDetailController()
        vc.orderId = orderId
        vc.popHandle = {url in
            
            if (url?.count == 0) {return}
            
            guard var vcs = controller.navigationController?.viewControllers else {return}
                        
            let webVC = XQWebController()
            webVC.url = XQNetworkManager.shared.getWebUrlAbsolutePath(path: url!)
            
            vcs.removeLast()
            vcs.append(webVC)
            
            controller.navigationController?.setViewControllers(vcs, animated: true)
        }
        controller.navigationController?.pushViewController(vc, animated: true)
    }
    
}
